package com.example.myapplication.z_b_details


import android.content.Intent
import androidx.fragment.app.Fragment
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.mvp.view.BaseFragment

import com.example.myapplication.R
import com.example.myapplication.adapter.FenleiImg_Adapter
import com.example.myapplication.adapter.Fenlei_Adapter
import com.example.myapplication.entity.Clothing_Entity
import com.example.myapplication.entity.Fenlei_Entity
import com.example.youhuo.entity.BaseEntity
import com.example.youhuo.mvp.contract.LjlContract
import com.example.youhuo.mvp.model.LjlModel
import com.example.youhuo.mvp.presenter.LjlPresenter
import com.example.youhuo.network.HttpType

/**
 * A simple [Fragment] subclass.
 */
class DetailsFragment : BaseFragment<LjlPresenter>(), LjlContract.LjlView  {

    private val twoRecLeft: RecyclerView by lazy { view1.findViewById<RecyclerView>(R.id.two_rec_left) }
    private val twoRecRight: RecyclerView by lazy { view1.findViewById<RecyclerView>(R.id.two_rec_right) }
    var parmas: ArrayList<Map<String,Any>>?= null
    var fenleiAdapter: Fenlei_Adapter?= null
    var fenleiimgAdapter: FenleiImg_Adapter?= null

    override fun findlayout(): Int {
        return R.layout.fragment_fenlei

    }
    override fun initView() {
        parmas = ArrayList()

    }

    override fun initData() {
        myprefrag = LjlPresenter(LjlModel(),this)
        var map : HashMap<String,Any> ?= HashMap()
        map!!.put("code", HttpType.FENLEI)
        map!!.put("menuid","1")
        parmas!!.add(map)

        myprefrag.TabData(parmas!!)
        var map1:HashMap<String,Any>?= HashMap()
        map1!!.put("code",HttpType.FENLEIImage)

        map1!!.put("categoryid",1)
        parmas!!.add(map1)
        myprefrag.TabData(parmas!!)

    }

    override fun showmsg(entity: BaseEntity) {
        if (entity is Fenlei_Entity){
            var fenleiEntity = entity as Fenlei_Entity
            var values = fenleiEntity.values
            fenleiAdapter = Fenlei_Adapter(R.layout.item,entity.values as ArrayList<Fenlei_Entity.data>)
            twoRecLeft.adapter = fenleiAdapter
            twoRecLeft.layoutManager = LinearLayoutManager(context)

            fenleiAdapter!!.setOnItemClickListener(object :BaseQuickAdapter.OnItemClickListener{
                override fun onItemClick(
                    adapter: BaseQuickAdapter<*, *>,
                    view: View,
                    position: Int
                ) {
                    var map:HashMap<String,Any>?= HashMap()
                    map!!.put("code",HttpType.FENLEIImage)
                    map!!.put("categoryid",position)
                    parmas!!.add(map)
                    myprefrag.TabData(parmas!!)
                }

            })
        }
        if ( entity is Clothing_Entity) {
            var clothingEntity = entity as Clothing_Entity
            var values = clothingEntity.values
            fenleiimgAdapter = FenleiImg_Adapter(R.layout.item2,values as ArrayList<Clothing_Entity.data>)
            twoRecRight.adapter = fenleiimgAdapter
            twoRecRight.layoutManager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)

            fenleiimgAdapter!!.setOnItemClickListener(object :BaseQuickAdapter.OnItemClickListener{
                override fun onItemClick(
                    adapter: BaseQuickAdapter<*, *>?,
                    view: View?,
                    position: Int
                ) {
                    var intent = Intent(context,DetailsActivity::class.java)
                    intent.putExtra("name",values[position].name)
                    startActivity(intent)
                }

            })
        }


    }


}
